// pages/IDCard/index.js
const app = getApp();
var topOpacity = 0;
import drawQrcode from '../../utils/weapp.qrcode.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 组件参数设置，传递到组件
    defaultData: {
      title: "身份码", // 导航栏标题
      showHome: false, //是否显示回到首页按钮
      showBack: false, //是否显示返回按钮
      showLogo: false, //是否显示logo
      bgColor: 'transparent'
    },
    baseUrl: app.baseUrl,
    codeText: "",
    version: app.globalData.version,
    navBarHeight: app.globalData.navBarHeight,
    qrcodeURL: "",

    authData: {
      authType: '0', //0表示使用登录弹窗  1表示使用完善昵称头像弹窗 2表示直接弹出头像昵称选择框
      authUsername: "false", //是否已经授权头像昵称
    },
    showLogin: false, //是否显示授权弹窗
    showWsInfoDom: false, //直接显示头像昵称弹窗
    showPrivacyFlag: app.globalData.showPrivacyFlag,

    openid: '',
    shareTitle: '',
    shareImg: '',
    info: '',
    is_nickAndHead: 0,
    head: '',
    mySf: '',
    isGetZzBadge: 0, //是否获取了长者徽章
    isGetRzBadge: 0, //是否获取了入住徽章
    is_register: 0,
    houseInfo: [],
    qrcodeURLwdidth:200
  },
  rpx2px (rpx) {
    const { windowWidth, pixelRatio: dpr } = wx.getSystemInfoSync()
    const rate = windowWidth / 750
    return rate * rpx
  },
  drawImg(openid, yxSjc) {
    wx.showLoading({
      title: '身份码生成中...',
      mask: true
    });
    // const rpx2px=this.rpx2px(480)
    drawQrcode({
      width: this.data.qrcodeURLwdidth,
      height: this.data.qrcodeURLwdidth,
      // x: (rpx2px - 200) / 2,
      // y: (rpx2px - 200) / 2,
      canvasId: 'myQrcode',
      typeNumber: 10,
      text: this.data.codeText + "?small_openid=" + openid + "*" + yxSjc,
      callback(e) {
        console.log('e: ', e)
        wx.hideLoading();
      }
    });
  },
  //跳转一键开门
  openDoor() {
    wx.navigateTo({
      url: '/pages/IDCard/openDoor'
    });
  },
  //跳转认证或我的房产
  toRzOrMyHouse() {
    if (this.data.mySf == '游客') {
      wx.navigateTo({
        url: '/pages/my/ownerValidate'
      });
    } else {
      wx.navigateTo({
        url: '/pages/my/myHouse'
      });
    }
  },
  // 属性二维码
  updateCode() {
    this.getIdcardPageInfo(this.data.openid);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this;
    this.setData({
      qrcodeURLwdidth:this.rpx2px(400)
    })
    var openid = wx.getStorageSync('ly24_openid');
    if (openid == '') {
      //缓存中没有openid
      //获取所需页面信息
      wx.login({
        success: function (res) {
          if (res.code) {
            //发起网络请求
            wx.request({
              url: that.data.baseUrl + 'getOpenid.php',
              method: 'POST',
              data: {
                'code': res.code
              },
              header: {
                'content-type': 'application/x-www-form-urlencoded'
              },
              success: function (res) {
                //缓存微信openid
                wx.setStorageSync('ly24_openid', res.data.openid);
                that.setData({
                  openid: res.data.openid
                });

                //获取本页数据
                that.getIdcardPageInfo(res.data.openid, 0);
              },
              fail: function (res) {}
            });
          }
        }
      });
    } else {
      that.setData({
        openid: openid
      });

      //获取本页数据
      that.getIdcardPageInfo(openid, 0);
    }

    //调用分享
    that.getShareInfo();
  },

  //获取本业数据
  getIdcardPageInfo(openid, isFresh) {
    var that = this;
    wx.showLoading({
      title: '数据加载中...',
      mask: true
    })
    wx.request({
      url: that.data.baseUrl + 'getIdcardPageInfo.php',
      method: 'POST',
      data: {
        'openid': openid,
        'isFresh': isFresh
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        wx.hideLoading();
        if (res.data.status == 1) {
          //重新赋值后台请求数据
          that.setData({
            codeText: res.data.codeText,
            info: res.data.info,
            is_nickAndHead: res.data.is_nickAndHead,
            head: res.data.head,
            mySf: res.data.mySf,
            isGetZzBadge: res.data.isGetZzBadge,
            isGetRzBadge: res.data.isGetRzBadge,
            is_register: res.data.is_register,
            houseInfo: res.data.houseInfo,
            yxSjc: res.data.yxSjc
          });
          //生成个人码
          that.drawImg(openid, res.data.yxSjc);
        } else {
          app.showTip(res.data.msg);
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            });
          }, 2000);
        }
      },
      fail: function () {
        wx.hideLoading();
        app.showTip('抱歉,服务器繁忙');
        setTimeout(function () {
          wx.navigateBack({
            delta: 1
          });
        }, 2000);
      }
    });
  },

  //获取微信分享信息
  getShareInfo() {
    var that = this;
    wx.request({
      url: that.data.baseUrl + 'getShareInfo.php',
      method: 'POST',
      data: {

      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        that.setData({
          shareTitle: res.data.shareTitle,
          shareImg: res.data.shareImg
        });
      },
      fail: function (res) {}
    });
  },

  //处理完善信息--不包含隐私协议
  addWxInfo2() {
    this.setData({
      showWsInfoDom: true
    });
  },

  //接收子组件电话授权及头像昵称授权后后返回的值
  returnVal(e) {
    var that = this;
    //授权完头像昵称
    this.setData({
      is_nickAndHead: e.detail.is_nickAndHead,
      head: that.data.baseUrl + e.detail.head,
      //nickname: e.detail.nickname
    });

    app.showTip('个人信息已完善');
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        selected: 2,
      })
    }
    // 监听 tabBar 的变化
    this.checkTabBarBadge();
  },
  checkTabBarBadge: function () {
    // 获取当前所有的 tabBar 页面
    const pages = getCurrentPages();
    // 获取当前页面的实例
    const currentPage = pages[pages.length - 1];
    // 获取当前页面的路径
    const pagePath = currentPage.route;

    // 这里可以根据 pagePath 来判断当前是哪个 tabBar 页面，然后进行相应的操作
    // 例如，更换图片
    // 假设当前页面是 "pages/home/home"
    console.log(pagePath)
    if (pagePath === 'pages/IDCard/index') {
      // 执行更换图片的操作
      this.setData({
        selectedIconPath: 'images/home-selected.png'
      });
    } else {
      this.setData({
        selectedIconPath: 'images/mine-selected.png'
      });
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    var that = this;
    return {
      title: that.data.shareTitle,
      path: '/pages/index/index',
      imageUrl: that.data.shareImg,
      success: function (res) {

      },
      fail: function (res) {

      }
    }
  }
})